import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import cipher.io.CipherFile;
import cipher.io.CipherFileInputStream;
import cipher.io.CipherFileOutputStream;
import cipher.io.CipherRandomAccessFile;
import cipher.util.AES;
import cipher.util.CipherConst;
import cipher.util.IFCrypt;


public class Sample {

	
	
	public static void covnertFiletoCFile(String type,boolean usebuffer) throws Exception{
        
		IFCrypt cipher = new AES("abcdefg");
		File f1 = new File("/a");
		CipherFile sf1 = new CipherFile(new File("/a_new"), cipher);
		FileInputStream k =  new FileInputStream(f1);
		BufferedInputStream io1 =new BufferedInputStream( k);
		long ek = System.currentTimeMillis();
		if("RANDOM".equals(type)){
			CipherRandomAccessFile eio1 = new CipherRandomAccessFile(sf1, "rw");
			int c = 0;
			if(usebuffer){
				byte b[] =new byte[8192];
				while( (c=io1.read(b))>-1){
					eio1.write(b,0,c);
				}
			}else{
				while( (c=io1.read())>-1){
					eio1.write(c);
				}
					
			}
			eio1.close();
			io1.close();	
		}
		if("STREAM".equals(type)){
			CipherFileOutputStream eio1 = new CipherFileOutputStream(sf1);
			int c = 0;
			if(usebuffer){
				byte b[] =new byte[8192];
				while( (c=io1.read(b))>-1){
					eio1.write(b,0,c);
				}
			}else{
				while( (c=io1.read())>-1){
					eio1.write(c);
				}
					
			}
			eio1.close();
			io1.close();
		}else if("CHANNEL".equals(type)){
			FileChannel f = k.getChannel();
			FileChannel sf = null;
			if(usebuffer){
				sf = ( new CipherFileOutputStream(sf1)).getChannel();
			}else{
				sf = ( new CipherRandomAccessFile(sf1,"rw") ).getChannel();
			}
			f.transferTo(0, f.size(),sf);
			sf.close();
		}
		System.out.println("DISK  "+sf1.getPhysicalFile().length());
		System.out.println("LOGIC "+CipherConst.getEcodeFullValue(f1.length()));
		System.out.println(System.currentTimeMillis()-ek);
	}
	public static void readCFile(String type,boolean usebuffer) throws Exception{
		IFCrypt cipher = new AES("abcdefg");
		CipherFile sf1 = new CipherFile(new File("/a_new"), cipher);
		long ek = System.currentTimeMillis();
		int totalread=0;
		System.out.println("sf len "+sf1.length());
		if("RANDOM".equals(type)){
			CipherRandomAccessFile eio1 = new CipherRandomAccessFile(sf1, "rw");
			int c = 0;
			eio1.seek(8900);
			eio1.skipBytes(8900);
			eio1.read();
			if(usebuffer){
				byte b[] =new byte[81923];
				while( (c=eio1.read(b))>-1){
					totalread+=c;
				}
			}else{
				while( (c=eio1.read())>-1){
					totalread++;
				}
					
			}
			eio1.close();
		}
		if("STREAM".equals(type)){
			CipherFileInputStream eio1 = new CipherFileInputStream(sf1);
			int c = 0;
			eio1.skip(8900);
			eio1.skip(8900);
			eio1.read();
			if(usebuffer){
				byte b[] =new byte[81923];
				while( (c=eio1.read(b))>-1){
					totalread+=c;
			
				}
			}else{
				while( (c=eio1.read())>-1){
					totalread++;
				}
					
			}
			eio1.close();
		}else if("CHANNEL".equals(type)){
			FileChannel sf = null;
			if(usebuffer){
				sf = ( new CipherFileInputStream(sf1)).getChannel();
			}else{
				sf = ( new CipherRandomAccessFile(sf1,"rw") ).getChannel();
			}
			//sf.close();
			ByteBuffer all = ByteBuffer.allocateDirect(8192);
			//sf.position(8900);
//			sf.position(8900+8900+1);
			int c = 0;
			while ((c = sf.read(all))>-1){
				totalread+=c;
				all.clear();
			}
		}
		System.out.println("READ  "+totalread);
		System.out.println(System.currentTimeMillis()-ek);
	}
	public static void main(String args[]) throws Exception{
		
			File f = new File("//Volumes/xiphoid/eclipse-jee-helios-macosx-cocoa-x86_64.tar.gz");
			RandomAccessFile rf = new RandomAccessFile(f,"r");
			rf.seek(1024*1024*19);
			int r = rf.read();	
			System.out.println("read "+r);
	}
	
}
